<?php
declare (strict_types=1);

namespace app\model\home;

use app\common\model\BaseModel;
use app\common\model\Store;
use app\model\admin\Game;
use \libs\Log;

class Room extends BaseModel
{
    protected $pk = 'id';

    protected function initialize()
    {
        parent::initialize();
    }

    protected function getGamesArrAttr($value)
    {
        return Game::where('id', 'in', $value)->field('id,game_title,game_cover')->select() ?: [];
    }

    /**
     * @param $value
     * @return false|string[]
     * 图册
     */
    protected function getRoomAlbumAttr($value)
    {
        if (!empty($value)) {
            return explode(',', $value);
        }
    }

    /**
     * @param $value
     * @return mixed|string
     * 门店名称获取器
     */
    protected function getStoreNameAttr($value)
    {
        return Store::where('id', $value)->value('store_name') ?: '';
    }

    protected function getRoomTypeStrAttr($value)
    {
        $roomType = [
            1 => '普通包间',
            2 => '标准包间',
            3 => '豪华包间'
        ];
        return $roomType[$value];
    }

    /**
     * @param $value
     * 游戏名称获取器
     */
    protected function getGameTitleAttr($value)
    {
        if (!empty($value)) {
            return Game::where('id', 'in', $value)->column('game_title') ?: '';
        }
    }

    /**
     * 获得列表
     * @param bool $where
     * @param string $fields
     * @param string|null $order
     * @param int $page
     * @param int $pageSize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($where = true, $fields = '*', $order = null, $page = 1, $pageSize = 20, $params)
    {
        $data = [
            'count' => 0,
            'list' => []
        ];
//        if (empty($params)) {
//            return $this->returnRight($data);
//        }
        $count = $this->where($where)
            ->where(function ($query) use ($params) {
                if (!empty($params)) {
                    $query->where([$params]);
                }
            })
            ->count();
        if (empty($count)) {
            return $this->returnRight($data);
        }
        $order = is_null($order) ? $this->pk . ' desc' : $order;
        $list = $this->where($where)
            ->where(function ($query) use ($params) {
                if (!empty($params)) {
                    $query->where([$params]);
                }
            })
            ->field($fields)
            ->order($order)
            ->page($page, $pageSize)
            ->select();
        foreach ($list->toArray() as $item) {
            if (empty($item['room_album'])) {
                $item['room_album'] = $item['room_cover'];
            }
            $listArr[] = $item;
        }
        $data['count'] = $count;
        $data['list'] = $listArr;
        return $this->returnRight($data);
    }

    /**
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 详情
     */
    public function getOne($data)
    {
        $info = $this->where('id', $data['id'])
            ->field('room_title,room_type,room_type room_type_str,room_cover,store_id,store_id store_name,
            room_album,score,per_price,games gamesArr,start_time start_at,end_time end_at')
            ->find();
        empty($info['room_album']) && $info['room_album'] = $info['room_cover'];
        $info['praise_count'] = "10k";
        return $this->returnRight($info);
    }
}
